package org.wdzl.services.impl;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wdzl.enums.MsgActionEnum;
import org.wdzl.enums.MsgSignFlagEnum;
import org.wdzl.enums.SearchFriendsStatusEnum;
import org.wdzl.mapper.*;
import org.wdzl.netty.ChatMsg;
import org.wdzl.netty.DataContent;
import org.wdzl.netty.UserChanelRel;
import org.wdzl.pojo.FriendsRequest;
import org.wdzl.pojo.MyFriends;
import org.wdzl.pojo.User;
import org.wdzl.services.UserServices;
import org.wdzl.utils.JsonUtils;
import org.wdzl.utils.QRCodeUtils;
import org.wdzl.vo.FriendsRequestVO;
import org.wdzl.vo.MyFriendsVO;

import java.util.Date;
import java.util.List;

/**
 * @author ww
 * <p>
 * 用户服务
 */
@Service
public class UserServicesImpl implements UserServices {

    @Autowired
    UserMapper userMapper;
    @Autowired
    Sid sid;
    @Autowired
    QRCodeUtils qrCodeUtils;
    //    @Autowired
//    FastDFSClient fastDFSClient;
    @Autowired
    MyFriendsMapper myFriendsMapper;
    @Autowired
    FriendsRequestMapper friendsRequestMapper;
    @Autowired
    UserCustomMapper userMapperCustom;
    @Autowired
    ChatMsgMapper chatMsgMapper;

    @Override
    public User queryUserNameIsExit(String username) {
        User user = userMapper.queryUserNameIsExit(username);
        return user;
    }

    @Override
    public User insert(User user) {
        String userId = sid.nextShort();

        String qrCodeURL = "";
        // 方案一：使用FastDFS存储文件
        // 为每个注册用户生成一个唯一的二维码, 创建二维码对象信息, 上传文件
/*        String qrCodePath = "/usr/local/qrcode/" + userId + "qrcode.png";
        qrCodeUtils.createQRCode(qrCodePath, "bird_qrcode:" + user.getUsername());
        MultipartFile qrcodeFile = FileUtils.fileToMultipart(qrCodePath);
        try {
            qrCodeURL = fastDFSClient.uploadQRCode(qrcodeFile);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        // 方案二: 本地存储
        String destFile = "E:\\IDEProject\\Exercise\\owner\\im_bird_sys\\images\\qrcode\\" + userId + ".png";
        try {
            qrCodeUtils.createQRCode(destFile, "bird_qrcode:" + user.getUsername());
            qrCodeURL = "http://192.168.0.109:8080/im_bird_sys/qrcode/" + userId + ".png";
        } catch (Exception e) {
            e.printStackTrace();
        }

        user.setId(userId);
        user.setQrcode(qrCodeURL);
        userMapper.insert(user);
        return user;
    }


    @Override
    public Integer preconditionSearchFriends(String myUserId, String friendUserName) {
        User user = queryUserNameIsExit(friendUserName);
        //1.搜索的用户如果不存在，则返回【无此用户】
        if (user == null) {
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        //2.搜索的账号如果是你自己，则返回【不能添加自己】
        if (myUserId.equals(user.getId())) {
            return SearchFriendsStatusEnum.NOT_YOURSELF.status;
        }
        //3.搜索的朋友已经是你好友，返回【该用户已经是你的好友】
        MyFriends myFriend = new MyFriends();
        myFriend.setMyUserId(myUserId);
        myFriend.setMyFriendUserId(user.getId());
        MyFriends myF = myFriendsMapper.selectOneByExample(myFriend);
        if (myF != null) {
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }
        return SearchFriendsStatusEnum.SUCCESS.status;
    }

    @Override
    public void sendFriendRequest(String myUserId, String friendUserName) {
        User user = queryUserNameIsExit(friendUserName);
        MyFriends myFriend = new MyFriends();
        myFriend.setMyUserId(myUserId);
        myFriend.setMyFriendUserId(user.getId());
        MyFriends myF = myFriendsMapper.selectOneByExample(myFriend);
        if (myF == null) {
            FriendsRequest friendsRequest = new FriendsRequest();
            String requestId = sid.nextShort();
            friendsRequest.setId(requestId);
            friendsRequest.setSendUserId(myUserId);
            friendsRequest.setAcceptUserId(user.getId());
            friendsRequest.setRequestDateTime(new Date());
            friendsRequestMapper.insert(friendsRequest);
        }
    }

    @Override
    public List<FriendsRequestVO> queryFriendRequestList(String acceptUserId) {
        return userMapperCustom.queryFriendRequestList(acceptUserId);
    }

    @Override
    public void deleteFriendRequest(FriendsRequest friendsRequest) {
        friendsRequestMapper.deleteByFriendRequest(friendsRequest);
    }

    @Override
    public void passFriendRequest(String sendUserId, String acceptUserId) {
        // 进行双向好友数据保存
        saveFriends(sendUserId, acceptUserId);
        saveFriends(acceptUserId, sendUserId);

        // 删除好友请求表中的数据
        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setSendUserId(sendUserId);
        friendsRequest.setAcceptUserId(acceptUserId);
        deleteFriendRequest(friendsRequest);

        // websocket实现
        Channel sendChannel = UserChanelRel.get(sendUserId);
        if (sendChannel != null) {
            // 使用websocket 主动推送消息到请求发起者，更新他的通讯录列表为最新
            DataContent dataContent = new DataContent();
            dataContent.setAction(MsgActionEnum.PULL_FRIEND.type);
            // 消息的推送
            sendChannel.writeAndFlush(new TextWebSocketFrame(JsonUtils.objectToJson(dataContent)));
        }
    }

    /**
     * 通过好友请求并保存数据到my_friends 表中
     *
     * @param sendUserId
     * @param acceptUserId
     */
    private void saveFriends(String sendUserId, String acceptUserId) {
        MyFriends myFriends = new MyFriends();
        String recordId = sid.nextShort();

        myFriends.setId(recordId);
        myFriends.setMyUserId(sendUserId);
        myFriends.setMyFriendUserId(acceptUserId);

        myFriendsMapper.insert(myFriends);
    }

    @Override
    public List<MyFriendsVO> queryMyFriends(String userId) {
        return userMapperCustom.queryMyFriends(userId);
    }

    @Override
    public String saveMsg(ChatMsg chatMsg) {
        org.wdzl.pojo.ChatMsg msgDB = new org.wdzl.pojo.ChatMsg();
        String msgId = sid.nextShort();
        msgDB.setId(msgId);
        msgDB.setAcceptUserId(chatMsg.getReceiverId());
        msgDB.setSendUserId(chatMsg.getSenderId());
        msgDB.setCreateTime(new Date());
        msgDB.setSignFlag(MsgSignFlagEnum.unsign.type);
        msgDB.setMsg(chatMsg.getMsg());

        chatMsgMapper.insert(msgDB);

        return msgId;
    }

    @Override
    public void updateMsgSigned(List<String> msgIdList) {
        userMapperCustom.batchUpdateMsgSigned(msgIdList);
    }

    @Override
    public List<org.wdzl.pojo.ChatMsg> getUnReadMsgList(String acceptUserId) {
        List<org.wdzl.pojo.ChatMsg> result = chatMsgMapper.getUnReadMsgListByAcceptUid(acceptUserId);
        return result;
    }

}